package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/dish")
@Slf4j
@Api(tags = "菜品模块")
public class DishController {

    @Autowired
    private DishService dishService;

    @PostMapping
    @ApiOperation("新增菜品")
    public Result<String> save(@RequestBody DishDTO dishDTO){

        log.info("dishDTO={}", dishDTO);
        dishService.save(dishDTO);

        return Result.success();

    }

    @GetMapping("/page")
    @ApiOperation("分页")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO){
        log.info("dish={},{},{},{},{}",
                dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize(), dishPageQueryDTO.getStatus()
                ,dishPageQueryDTO.getName(), dishPageQueryDTO.getCategoryId());

        PageResult pageResult = dishService.page(dishPageQueryDTO);

        return Result.success(pageResult);
    }

    @PostMapping("/status/{status}")
    @ApiOperation("状态更新")
    public Result<String> updateStatus(@PathVariable Integer status, Long id){

        log.info("status={},id={}", status, id);
        dishService.updateStatus(status, id);
        return Result.success();

    }

    @GetMapping("{id}")
    @ApiOperation("回显")
    public Result<DishDTO> showOne(@PathVariable Long id){

        log.info("id={}", id);
        DishDTO dishDTO = dishService.findById(id);
        return Result.success(dishDTO);
    }

    @PutMapping
    @ApiOperation("修改")
    public Result<String> updateDish(@RequestBody DishDTO dishDTO){

        log.info("dishDto={}", dishDTO);

        dishService.update(dishDTO);

        return Result.success();

    }

    @DeleteMapping
    @ApiOperation("删除")
    public Result<String> delete(@RequestParam List<Long> ids){

        log.info("ids={}", ids);

        dishService.deleteById(ids);

        return Result.success();

    }

    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    public Result<List<Dish>> list(Long categoryId){
        List<Dish> list = dishService.list(categoryId);
        return Result.success(list);
    }


}
